<template>
  <el-card class="mt-20" shadow="hover">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="addSpu"
      >添加SPU</el-button
    >
    <el-table :data="spuList" border class="mt-20" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" width="60" />
      <el-table-column prop="spuName" label="SPU名称" />
      <el-table-column prop="description" label="SPU描述"> </el-table-column>
      <el-table-column prop="operator" label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Plus"
            title="添加SKU"
            @click="addSku(row)"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            title="修改SPU"
            @click="updateSpu(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            :icon="InfoFilled"
            title="查看SKU列表"
            @click="getSkuList(row.id, row.spuName)"
          ></el-button>
          <el-popconfirm title="您确认要删除吗?">
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :icon="Delete"
                title="删除SPU"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <!-- 
         @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
       -->
    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
    />
  </el-card>

  <el-dialog :title="`${spuName}的SKU列表`" v-model="dialogVisiable">
    <el-table :data="skuList" border max-height="300" v-loading="skuLoading">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="价格（元）" prop="price"></el-table-column>
      <el-table-column label="重量（KG）" prop="weight"></el-table-column>
      <el-table-column label="默认图片" prop="skuDefaultImg">
        <template v-slot="{ row }">
          <img :src="row.skuDefaultImg" :alt="row.skuName" class="sku__img" />
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { ref, watch } from "vue";
import { GetSpuListApi } from "@/api/product/spu";
import { GetSkuListApi } from "@/api/product/sku";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import type { SkuList } from "@/api/product/model/skuModel";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";

// 当前页码
const currentPage = ref(1);
// 每页条数
const pageSize = ref(3);
// 总数
const total = ref(0);
// 加载效果
const loading = ref(false);

// 定义SPU列表数据
const spuList = ref<SpuList>([]);

// 是否显示对话框
const dialogVisiable = ref(false);

// 定义SKU列表数据
const skuList = ref<SkuList>([]);
const skuLoading = ref(false);
const spuName = ref("");

// 获取categoryStore
const categoryStore = useCategoryStore();
// 封装获取SPU列表函数
const getSpuList = async () => {
  loading.value = true;
  const res = await GetSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  // console.log(res);
  spuList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

// 监视三级分类的变化，获取SPU列表
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      total.value = 0;
      currentPage.value = 1;
      pageSize.value = 3;
      return;
    }
    getSpuList();
  },
  { immediate: true }
);

// 接收父组件传递的自定义事件
const emit = defineEmits(["setShowSpuList"]);

// 点击添加SPU
const addSpu = () => {
  emit("setShowSpuList", 2);
  spuStore.$reset();
};

// 获取spuStore
const spuStore = useSpuStore();
// 点击修改SPU
const updateSpu = (row: SpuItem) => {
  emit("setShowSpuList", 2);
  spuStore.spu = row;
};

// 点击添加Sku
const addSku = (row: SpuItem) => {
  emit("setShowSpuList", 3);
  spuStore.spu = row;
};

// 查看SKU列表
const getSkuList = async (spuId: number, name: string) => {
  skuLoading.value = true;
  dialogVisiable.value = true;
  spuName.value = name;
  skuList.value = await GetSkuListApi(spuId);
  skuLoading.value = false;
};
</script>

<style scoped>
.sku__img {
  width: 100px;
  height: 100px;
}
</style>
